export default {
  name: "changeCity",
  props: {
    citys: Array,
    level: String
  },
  data() {
    return {
      value: [],
      cityLists: [], 
      currentData: [],
      cityProps: {
        value: "code",
        label: "name",
        children: "subLevelModelList"
      },
      currentUserLevel: JSON.parse(localStorage.getItem('userinfo')).level, //获取当前用户等级
      currentCitys: [],
      disabled: false
    }
  },
  created() {
    this.value = this.citys;
    let userinfo = JSON.parse(localStorage.getItem('userinfo'));
    let province = +userinfo.province || '';
    let city = +userinfo.city || '';
    let area = +userinfo.area || '';
    this.currentCitys = [];
    this.currentCitys.push(province, city, area);
    this.getCitys()
    this.disabled = this.currentUserLevel === '3' ? true : false;
    this.handleCityLists()
  },
  watch: {
    citys(newVal) {
      this.value = newVal;
    },
    level(newVal) {
      if (newVal == 0) {
        this.cityLists = [];
        var obj = {
          code: "全国",
          name: "全国",
        }
        this.cityLists.push(obj)
        this.currentData = ['全国'];
        return;
      }
      this.currentData = [];
      this.getCitys()
    }
  },
  methods: {
    getCitys() {
      this.$http.get("assets/citys.json").then(res => {
        let cityLists = res.body.city;
        this.cityLists = [];
        if (this.level === '0') {
          this.cityLists = [];
          var obj = {
            code: "全国",
            name: "全国",
          }
          this.cityLists.push(obj)
          this.currentData = ['全国'];
          return;
        }
        if (this.currentUserLevel === '1') {
          cityLists.forEach(el => {
            if (el.code == this.currentCitys[0]) {
              this.cityLists.push(el);
              return;
            }
          });
        } else if (this.currentUserLevel === '2') {
          let tempObj = {};
          cityLists.forEach(el => {
            if (el.code === this.currentCitys[0]) {
              Object.assign(tempObj, el);
            }
          });
          tempObj.subLevelModelList.forEach(el => {
            if (el.code === this.currentCitys[1]) {
              tempObj.subLevelModelList = [];
              tempObj.subLevelModelList.push(el);
              this.cityLists.push(tempObj);
              return;
            }
          })
        } else {
          this.cityLists = cityLists;
        }
        this.handleCityLists(this.level)
      })
    },
    // // 处理城市数据
    handleCityLists(level) {
      if (level === "1") {
        this.cityLists.forEach(el => {
          // delete el.subLevelModelList
          this.$delete(el,"subLevelModelList")
        })
      } else if (level === "2") {
        this.cityLists.forEach(el => {
          el.subLevelModelList.forEach(item => {
            // delete item.subLevelModelList
            this.$delete(item,"subLevelModelList")
          })
        })
      }
    },
    handleChange(value) {
      this.$emit("handleChange", value)
    }
  }
}